package exercises;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by ravindra on 18/7/14.
 */
public class DrivePrimeFactors {
    public static void main(String[] Args){
        int number;
        Console console = new Console();
        number = console.readInput("Enter No....");

        PrimeFactors primefactors = new PrimeFactors();
        List<Integer> primeFactorsList = primefactors.generate(number);

        System.out.println(primeFactorsList);

    }

}

class PrimeFactors{
    private Integer[] primes;
    private ArrayList<Integer> primeNumberList;
    private int primeNumberListPointer;

    PrimeFactors() {
        primes = new Integer[]{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
                                43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
                                101, 103, 107, 109, 113, 127, 131, 137, 139, 149,
                                151, 157, 163, 167, 173, 179, 181, 191, 193, 197,199};
        primeNumberList = new ArrayList<Integer>(Arrays.asList(primes));
        primeNumberListPointer = 0;

    }

    int getPrimeNumber(int primeNumberListPointer){
        int primeNumber = 1;

        if(primeNumberListPointer < primeNumberList.size())
            primeNumber = primeNumberList.get(primeNumberListPointer);

        return primeNumber;
    }

    ArrayList<Integer> generate(int n){
        ArrayList<Integer> primeFactors = new ArrayList<Integer>();
        int primeFactor = getPrimeNumber(primeNumberListPointer++);

        while(n != 1){
            if(n % primeFactor == 0){
                n /= primeFactor;
                if(!primeFactors.contains(primeFactor))
                    primeFactors.add(primeFactor);
                primeNumberListPointer = 0;
            }
            primeFactor = getPrimeNumber(primeNumberListPointer++);


        }

        return primeFactors;
    }
}


class Console{

    int readInput(String message){
        int input = 0;
        System.out.print(message);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        try {
            input = Integer.parseInt(br.readLine());
        } catch (IOException e) {
            e.printStackTrace();
        }

        return input;
    }
}
